<?php
namespace Project_725\Model;

use TwinPeaks\Core as Core;

class Invitation extends Base
{
	const CODE_LENGTH = 24;
	
	const STATUS_PENDING = 'pending';
	const STATUS_ACCEPTED = 'accepted';
	const STATUS_REJECTED = 'rejected';

	/**
	 * @var \Zend\Db\TableGateway\TableGateway
	 */
	static protected $_tableGW;
	
	/**
	 * @return void
	 */
	protected function init($tableName = null)
	{
		parent::init('invitation', 'code');
	}
	
	/**
	 * @return void
	 */
	static function create($senderId, $recipientId, $senderName, $recipientName, $code)
	{
		static::init();
		$data = ['sender_id' => $senderId, 'recipient_id' => $recipientId,
			'sender_name' => $senderName, 'recipient_name' => $recipientName,
			'code' => $code
		];
		static::$_tableGW->insert($data);

		return self::getByCode($code);
	}

	/**
	 * @return string
	 */
	static function generateCode()
	{
		return Core\get_unique_id(self::CODE_LENGTH);		
	}
	
	/**
	 * @return Invitation|null
	 */
	static function getByCode($code)
	{
		return self::getByColumn('code', $code);
	}
	
	/**
	 * @return
	 */
	static function getBySenderId($id)
	{
		return self::getByColumn('sender_id', $id);
	}
	
	/**
	 * @return boolean
	 */
	final function isRejected()
	{
		return $this->dbTabRow->status == self::STATUS_REJECTED;
	}
	
	/**
	 * @return boolean
	 */
	final function isAccepted()
	{
		return $this->dbTabRow->status == self::STATUS_ACCEPTED;
	}
}
